{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 第 7 节　参数估计\n",
    "## 第 3 章　使用 Python 进行数据分析｜用 Python 动手学统计学"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2. 环境准备"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "# 用于数值计算的库\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "# 用于绘图的库\n",
    "import seaborn as sns\n",
    "from scipy import stats\n",
    "\n",
    "sns.set()\n",
    "\n",
    "# 设置浮点数打印精度\n",
    "%precision 3\n",
    "# 在 Jupyter Notebook 里显示图形\n",
    "%matplotlib inline"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T17:35:23.203623Z",
     "end_time": "2024-04-16T17:35:23.217887Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "0    4.352982\n1    3.735304\n2    5.944617\n3    3.798326\n4    4.087688\n5    5.265985\n6    3.272614\n7    3.526691\n8    4.150083\n9    3.736104\nName: length, dtype: float64"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读入数据\n",
    "fish = pd.read_csv(\"3-7-1-fish_length.csv\")[\"length\"]\n",
    "fish"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T17:35:23.220887Z",
     "end_time": "2024-04-16T17:35:23.229750Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 4. 实现：点估计"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "4.187"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 总体均值的点估计\n",
    "mu = np.mean(fish)\n",
    "mu"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T17:35:23.229750Z",
     "end_time": "2024-04-16T17:35:23.236273Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "0.680"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 总体方差的点估计\n",
    "sigma_2 = np.var(fish, ddof=1)\n",
    "sigma_2"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T17:35:23.237274Z",
     "end_time": "2024-04-16T17:35:23.241894Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 9. 实现：区间估计"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "9"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 自由度\n",
    "df = len(fish) - 1\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T17:35:23.241894Z",
     "end_time": "2024-04-16T17:35:23.247282Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "0.261"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 标准误差\n",
    "sigma = np.sqrt(sigma_2)\n",
    "se = sigma / np.sqrt(len(fish))\n",
    "se"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T17:35:23.246282Z",
     "end_time": "2024-04-16T17:35:23.315770Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "(3.597, 4.777)"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 区间估计\n",
    "interval = stats.t.interval(0.95, df=df, loc=mu, scale=se)\n",
    "interval"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T17:35:23.253333Z",
     "end_time": "2024-04-16T17:35:23.316770Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 10. 补充：置信区间的求解细节"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "2.262"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 97.5% 分位数\n",
    "t_975 = stats.t.ppf(q=0.975, df=df)\n",
    "t_975"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T17:35:23.276304Z",
     "end_time": "2024-04-16T17:35:23.316770Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "3.597"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 下置信界限\n",
    "lower = mu - t_975 * se\n",
    "lower"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T17:35:23.282804Z",
     "end_time": "2024-04-16T17:35:23.316770Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "4.777"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 上置信界限\n",
    "upper = mu + t_975 * se\n",
    "upper"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T17:35:23.289524Z",
     "end_time": "2024-04-16T17:35:23.316770Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 11. 决定置信区间大小的因素"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "(-1.713, 10.087)"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 样本方差越大, 置信区间越大\n",
    "se2 = (sigma * 10) / np.sqrt(len(fish))\n",
    "stats.t.interval(0.95, df=df, loc=mu, scale=se2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T17:35:23.293871Z",
     "end_time": "2024-04-16T17:35:23.317770Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "(4.023, 4.351)"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 样本容量越大, 置信区间越小\n",
    "df2 = (len(fish) * 10) - 1\n",
    "se3 = sigma / np.sqrt(len(fish) * 10)\n",
    "stats.t.interval(0.95, df=df2, loc=mu, scale=se3)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T17:35:23.303629Z",
     "end_time": "2024-04-16T17:35:23.317939Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "(3.339, 5.035)"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 99% 置信区间\n",
    "stats.t.interval(0.99, df=df, loc=mu, scale=se)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T17:35:23.309698Z",
     "end_time": "2024-04-16T17:35:23.318444Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 12. 区间估计结果的解读"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "array([False, False, False, ..., False, False, False])"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果置信区间包含总体均值 (4) 就取 True\n",
    "be_included_array = np.zeros(20000, dtype=\"bool\")\n",
    "be_included_array"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T17:35:23.315770Z",
     "end_time": "2024-04-16T17:35:23.334450Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [],
   "source": [
    "# 执行 20,000 次求 95% 置信区间的操作\n",
    "# 如果置信区间包含总体均值 (4) 就取 True\n",
    "np.random.seed(1)\n",
    "norm_dist = stats.norm(loc=4, scale=0.8)\n",
    "for i in range(0, 20000):\n",
    "    sample = norm_dist.rvs(size=10)\n",
    "    df = len(sample) - 1\n",
    "    mu = np.mean(sample)\n",
    "    std = np.std(sample, ddof=1)\n",
    "    se = std / np.sqrt(len(sample))\n",
    "    interval = stats.t.interval(0.95, df, mu, se)\n",
    "    if (interval[0] <= 4 and interval[1] >= 4):\n",
    "        be_included_array[i] = True"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T17:35:23.320448Z",
     "end_time": "2024-04-16T17:35:29.070961Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "0.948"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(be_included_array) / len(be_included_array)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T17:35:29.064858Z",
     "end_time": "2024-04-16T17:35:29.073829Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T17:35:29.071954Z",
     "end_time": "2024-04-16T17:35:29.075841Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
